package main

import (
	"bytes"
	"mime/multipart"
	"fmt"
	"os"
	"io"
	"net/http"
	"io/ioutil"
)

func  postFile(filename string,targetUrl string) error  {
	bodyBuf := &bytes.Buffer{}
	bodyWriter := multipart.NewWriter(bodyBuf)

	fileWriter,err := bodyWriter.CreateFormFile("uploadfile",filename)
	if err !=nil{
		fmt.Println("error writing to buffer")
		return  err
	}

	fh,err := os.Open(filename)
	if err !=nil{
		fmt.Println("error open file")
		return  err
	}
	defer fh.Close()

	_,err := io.Copy(fileWriter,fh)
	if err!=nil{
		return  err
	}

	contentType := bodyWriter.FormDataContentType()
	bodyWriter.Close()

	resp,err := http.Post(targetUrl,contentType,bodyBuf)
	if err !=nil{
		return err
	}
	defer resp.Body.Close()

	respbody, err := ioutil.ReadAll(resp.Body)
	if err!=nil{
		return  err
	}
	fmt.Println(resp.Status)
	fmt.Println(string(respbody))
	return nil
}

func main(){
	targetUrl := "http://localhost:9090/upload"
	filename := "upload.gtpl"
	postFile(filename,targetUrl)
}
